﻿// Copyright @ shch8.com All Rights Reserved At 2008-2-29
//开发：商创技术（www.shch8.com）望月狼
/*
·绘图。

*/
package com._public._display{
	import flash.display.*;
	import flash.geom.Point;
	public class DrawShape extends Sprite {
		/*
		 * 画线条*/
		public static function drawline(graphics:Graphics,x1:Number=0,y1:Number=0,x2:Number=50,y2:Number=0,col:uint=0xffffff,type:Number=1):void {
			graphics.lineStyle(type,col);
			graphics.moveTo(x1,y1);//绘制点,初始时都为0
			graphics.lineTo(x2,y2);
		}
		
		/*
		 * 画三角形*/
		public static function triangle(graphics:Graphics,x1:Number=0,y1:Number=0,x2:Number=50,y2:Number=0,col:uint=0xffffff,type:Number=1,showLine:Boolean=false):void {
			graphics.lineStyle(1,col);
			graphics.beginFill(col);
			graphics.moveTo(0,0);//绘制点,初始时都为0
			graphics.lineTo(5,5);
			graphics.lineTo(0,10);
			graphics.lineTo(0,0);
			graphics.endFill();
		}
		
		   /**
	   * 画虚线
	   * @author WZH(shch8.com)
	   * @param graphics <b>Graphics</b> 
	   * @param beginPoint <b>Point</b> 开始位置
	   * @param endPoint <b>Point</b> 结束位置
	   * @param widths <b>Number</b> 虚线的长度
	   * @param grap <b>Number</b> 间距
	   * @example
	   * sp=new Shape ;
		sp.graphics.lineStyle(1,0XFF0000);
		addChild(sp);
		BrokenLine.drawDashed(sp.graphics,new Point(100,100),new Point(400,400),5,2);
	   */
		 static public function drawDashed(graphics:Graphics,beginPoint:Point,endPoint:Point,widths:Number=5,grap:Number=2):void
		   {
			if (! graphics || ! beginPoint || ! endPoint || widths <= 0 || grap <= 0)return;
			var Ox:Number=beginPoint.x;
			var Oy:Number=beginPoint.y;
			var radian:Number=Math.atan2(endPoint.y - Oy,endPoint.x - Ox);
			var totalLen:Number=Point.distance(beginPoint,endPoint);
			var currLen:Number=0;
			var x:Number,y:Number;

			while (currLen <= totalLen)
			{
			 x=Ox + Math.cos(radian) * currLen;
			 y=Oy + Math.sin(radian) * currLen;
			 graphics.moveTo(x,y);

			 currLen+= widths;
			 if (currLen > totalLen)
			 {
			  currLen=totalLen;
			 }

			 x=Ox + Math.cos(radian) * currLen;
			 y=Oy + Math.sin(radian) * currLen;
			 graphics.lineTo(x,y);

			 currLen+= grap;
			}
		   }
	}
}